package com.leo.quartz.controller.quartz;

import com.leo.base.AbstractController;
import com.leo.base.BaseResponse;
import com.leo.base.PageResponse;
import com.leo.base.ReturnResponse;
import com.leo.quartz.common.lang.PageView;
import com.leo.quartz.common.lang.ReturnCodeConstant;
import com.leo.quartz.common.request.MyJobRequestParam;
import com.leo.quartz.common.response.task.MyJobVO;
import com.leo.quartz.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 任务的添加修改
 *
 * @author leo
 * @version 1.0
 * @date 2018/09/13 23:45
 */
@Controller
@RequestMapping("/quartz")
public class QuartzJobController extends AbstractController {

    private JobService jobService;
    private ReturnResponse response;

    @RequestMapping("/jobList.html")
    public ModelAndView jobListView() {
        return this.buildModelAndView(PageView.Quartz.JOB_LIST);
    }

    @RequestMapping("/editJob.html")
    public ModelAndView jobEditView(Long id) {
        try {
            response = jobService.getJobById(id);
            return this.buildModelAndView(PageView.Quartz.JOB_EDIT, response);
        } catch (Exception ex) {
            return this.buildModelAndView(PageView.ERROR_404);
        }
    }

    @RequestMapping("/addJob.html")
    public ModelAndView addView(Long id) {
        return this.buildModelAndView(PageView.Quartz.JOB_CREATE);
    }

    @RequestMapping("/jobList")
    @ResponseBody
    public String queryJobList(MyJobRequestParam requestParam) {
        response = jobService.queryMyJobList(requestParam);
        return buildResult(response);
    }

    @RequestMapping("/removeJob")
    @ResponseBody
    private String removeJob(Long id) {
        try {
            response = jobService.removeJob(id);
        } catch (Exception ex) {
            response = failure(ReturnCodeConstant.FAILURE_CODE, ex.getMessage());
        }
        return buildResult(response);
    }

    @RequestMapping("addJob")
    @ResponseBody
    private String addJob(MyJobRequestParam param) {
        try {
            response = jobService.addJob(param);
        } catch (Exception ex) {
            response = failure(ReturnCodeConstant.FAILURE_CODE, ex.getMessage());
        }
        return buildResult(response);
    }

    @RequestMapping("editJob")
    @ResponseBody
    private String editJob(MyJobRequestParam param) {
        try {
            response = jobService.editJob(param);
        } catch (Exception ex) {
            response = failure(ReturnCodeConstant.FAILURE_CODE, ex.getMessage());
        }
        return buildResult(response);
    }


    public JobService getJobService() {
        return jobService;
    }

    @Autowired
    public void setJobService(JobService jobService) {
        this.jobService = jobService;
    }
}
